package com.attendance.chdingtask.utils;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @ClassName: DateUtils
 * @Description 日期 工具类
 * @Author Mr.Bu
 * @Date 2019/12/11 13:01
 * @Version: 1.0
 */
public class DateUtils {

    public static final Date dateTime(final String format, final String ts) {
        try {
            return new SimpleDateFormat(format).parse(ts);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }


    //月份函数
    public static String monthUpAndDown(Integer month) {

        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=new Date();

        Calendar calendar=Calendar.getInstance();
        calendar.setTime(date); // 设置为当前时间
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + month); // 设置为上一个月
        date=calendar.getTime();

        return dateFormat.format(date);
    }

    //获取 小时前后的 时间戳
    public static String getBeforeDate(Integer hour) {

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c=Calendar.getInstance();
//        System.out.println(sdf.format(c.getTime()));
        c.add(Calendar.HOUR_OF_DAY, hour);//1小时前
        String hour_=sdf.format(c.getTime());
        return hour_;
    }

    //获取当前系统前一天日期
    public static String getNextDay(Integer day) {

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c=Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, day);
        String day_=sdf.format(c.getTime());
        return day_;
    }

    //获取当前日期
    public static String getNowDay() {

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=new Date();
        return sdf.format(date);
    }

    public static String getDayToString(Date date) {

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }

    public static String getDayToNyr(Date date) {

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }

    public static String getDateFormByString(String bkdate) {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(Long.parseLong(bkdate));
    }

    public static Date getDateByString(String string) throws ParseException {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.parse(string);
    }

    /**
     * 获取当前系统前一天日期 yyyy-MM-dd
     *
     * @param day
     * @return
     */
    public static String getBerforDay(Integer day) {

        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        Calendar c=Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, day);
        String day_=sdf.format(c.getTime());
        return day_;
    }
    public static Date getAftterDate(){
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        return  calendar.getTime();
    }


    public static void main(String[] args) {
        Date date=new Date();
        String str=getDayToString(date);
        System.out.println(str.substring(0,4));
        System.out.println(str.substring(5,7));
        System.out.println(str.substring(11,str.length()));
    }


}
